package cn.itcast.nems.order.web.controller.b;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.order.web.vo.ProductPriceRulePageQueryVO;
import cn.itcast.nems.order.web.vo.ProductPriceRuleVO;
import cn.itcast.nems.product.dao.dataobject.ProductPriceDO;
import cn.itcast.nems.product.dao.entity.ProductPriceUpdateTask;
import cn.itcast.nems.product.service.ProductPriceUpdateTaskService;
import cn.itcast.nems.product.service.ProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程价格
 * </p>
 *
 * @author zhouwei
 * @since 2023/9/1 5:05 PM
 **/
@RestController
@RequestMapping("/b/product_price")
@Slf4j
public class ProductPriceRuleController {
    @Autowired
    private ProductPriceUpdateTaskService productPriceUpdateTaskService;
    @Autowired
    private ProductService productService;

    /**
     * 获取最后更新时间
     *
     * @return 更新时间
     **/
    @GetMapping("/last_update_time")
    public ResponseEntity<?> findLastUpdateTime() {
        ProductPriceUpdateTask productPriceUpdateTask = productPriceUpdateTaskService.findLastOne();
        String time = Objects.isNull(productPriceUpdateTask) ? null :
                LocalDateTimeUtil.formatNormal(productPriceUpdateTask.getCreatedDatetime());
        return ResponseEntity.ok().body(time);
    }

    /**
     * 分页查询
     *
     * @param queryParam 请求参数
     * @return 分页数据
     **/
    @PostMapping("/page/list")
    public ResponseEntity<?> findPageList(@RequestBody BasePageRequest<ProductPriceRulePageQueryVO> queryParam) {
        queryParam.convert();
        ProductPriceDO priceDO = new ProductPriceDO();
        if (Objects.nonNull(queryParam.getCondition())) {
            BeanUtils.copyProperties(queryParam.getCondition(),priceDO );
        }
        log.info("[课程价格分页查询] 请求参数: {} ", queryParam);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ProductPriceDO> page
                = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryParam.getPageNumber());
        page.setSize(queryParam.getPageSize());
        IPage<ProductPriceDO> data = productService.findPageList(page, priceDO);
        List<ProductPriceRuleVO> list = data.getRecords().stream().map(item -> {
                ProductPriceRuleVO priceRuleVO = new ProductPriceRuleVO();
                BeanUtils.copyProperties(item, priceRuleVO);
                // 班级名称 ,如果 biz_no 存在阶段 , 需要追加
                // priceRuleVO.setName(ProductBeanUtil.getClassName(item.getName(), item.getCode()));
                priceRuleVO.setName(item.getName());
                priceRuleVO.setEffectiveDate(CustomBeanUtil.append(" - ", CustomBeanUtil.format(item.getEffectiveDatetime()),
                        CustomBeanUtil.format(item.getExpireDatetime())));
                String price = Objects.isNull(item.getPrice()) ? null :
                        NumberUtil.format(BigDecimal.valueOf(item.getPrice()).divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP),2,",");
                priceRuleVO.setPrice(price);
                String postpaidPrice = Objects.isNull(item.getPostpaidPrice()) ? null :
                        NumberUtil.format(BigDecimal.valueOf(item.getPostpaidPrice()).divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP),2 ,",");
                priceRuleVO.setPostpaidPrice(postpaidPrice);
                return priceRuleVO;
            }
        ).toList();
        Page<ProductPriceRuleVO> pageData = Page.of(Long.valueOf(data.getCurrent()).intValue(),
                Long.valueOf(data.getSize()).intValue(),
                Long.valueOf(data.getTotal()).intValue(), list);
        return ResponseEntity.ok().body(pageData);
    }
}
